package kau.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Board;

import org.apache.ibatis.session.SqlSession;

public class BoardDAOImpl implements BoardDAOIF {

	DBUtil db = null;
	private static BoardDAOImpl instance;

	private BoardDAOImpl() {
		db = DBUtil.getInstance();
	}

	public static BoardDAOImpl getInstance() {
		if (instance == null)
			instance = new BoardDAOImpl();
		return instance;
	}

	private boolean isExist(int bNumber) {
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try {
			result = session.selectOne("dbConfig.BoardMapper.isExist", bNumber);

		} finally {
			if (session != null)
				session.close();
		}
		if (result != null)
			flag = true;

		return flag;
	}

	@Override
	public void insert(Board board) throws DuplicationException {
		if (isExist(board.getbNumber()))
			throw new DuplicationException(board.getTitle());
		SqlSession session = db.getSession();
		try {
			session.insert("dbConfig.BoardMapper.insert", board);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public void update(Board board) throws RecordNotFoundException {
		if (!isExist(board.getbNumber()))
			throw new RecordNotFoundException(board.getTitle());
		SqlSession session = db.getSession();
		try {
			session.update("dbConfig.BoardMapper.update", board);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public void delete(int bNumber, int courseId)
			throws RecordNotFoundException {
		if (!isExist(bNumber))
			throw new RecordNotFoundException();
		SqlSession session = db.getSession();
		HashMap<String, Integer> param = new HashMap<String, Integer>();
		param.put("bNumber", bNumber);
		param.put("courseId", courseId);
		try {
			session.delete("dbConfig.BoardMapper.delete", param);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
	}

	@Override
	public Board getBoard(int bNumber) throws RecordNotFoundException {
		if (!isExist(bNumber))
			throw new RecordNotFoundException();
		SqlSession session = db.getSession();
		Board board = null;
		try {
			board = session.selectOne("dbConfig.BoardMapper.getBoard", bNumber);
		} finally {
			if (session != null)
				session.close();
		}

		return board;
	}
	
	

	@Override
	public Board getBoard(int courseId, String title) {
		// TODO Auto-generated method stub
		Board board=null;
		System.out.println("corseId "+courseId + " title "+title);
		
		SqlSession session = db.getSession();
		HashMap map=new HashMap();
		map.put("courseId", courseId);
		map.put("title", title);
		
		
		try {
			board=session.selectOne("dbConfig.BoardMapper.getBoard2",map);
			
		} finally {
			if (session != null)
				session.close();
		}
		
		
		return board;
	}

	@Override
	public ArrayList<Board> getAllBoards() {
		ArrayList<Board> boardList = new ArrayList<Board>();
		SqlSession session = db.getSession();
		try {
			boardList = (ArrayList) session
					.selectList("dbConfig.BoardMapper.getAllBoards");
		} finally {
			if (session != null)
				session.close();
		}
		return boardList;
	}

	public boolean boardNumberCheck(int bNumber) {
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try {
			result = session.selectOne("dbConfig.BoardMapper.isExist", bNumber);
		} finally {
			session.close();
		}
		if (result != null)
			flag = true;

		return flag;
	}

	@Override
	public void boardUpdatePermission(Board board)
			throws RecordNotFoundException {
		// String courseId, String custId
		if (!isExist(board.getbNumber()))
			throw new RecordNotFoundException(board.getTitle());
		SqlSession session = db.getSession();
		try {
			session.update("dbConfig.PermissionMapper.boardUpdatePermission",
					board);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

	}

	@Override
	public int getMaxbNumber() {
		int listCount=0;
		SqlSession session = db.getSession();
		
		try {
			Integer temp=session.selectOne("dbConfig.BoardMapper.getMaxbNumber");
			if(temp!=null)listCount=temp;
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}
		return listCount;
	}


	
}